/*
Created By VietRise on 4/6/2014
*/

#include "Mouse.h"
#include "Scenes\ScreenGameplay.h"

CCAction* Mouse::_appear = NULL;
CCAction* Mouse::_disappear = NULL;
CCAction* Mouse::_dizzy = NULL;

Mouse::~Mouse()
{
	CC_SAFE_RELEASE(_appear);
	CC_SAFE_RELEASE(_disappear);
	CC_SAFE_RELEASE(_dizzy);
}

Mouse::Mouse(ScreenGameplay* game)
{
	_game = game;	
	_canHit = false;
	_atHole = -1;
	_timeAppear = 0;
	_timeDelay = 0;
	_countTimeDelay = 0;
	this->initWithSpriteFrameName("obj_mouse_05.png");
	this->setVisible(false);
}

Mouse* Mouse::create(ScreenGameplay* game)
{
	Mouse* sprite = new Mouse(game);
	if (sprite)
	{
		sprite->autorelease();
		return sprite;
	}

	CC_SAFE_DELETE(sprite);
	return NULL;
}

void Mouse::createAction()
{
	// Action Appear
	CCAnimation* animation;
	animation = CCAnimation::create();
	CCSpriteFrame* frame;
	for (int i = 1; i<=5; i++)
	{
		char szName[100] = { 0 };
		sprintf(szName, "obj_mouse_0%i.png", i);
		frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(szName);
		animation->addSpriteFrame(frame);
	}

	animation->setDelayPerUnit(TIME_ANIMATION_MOUSE / 5);
	animation->setRestoreOriginalFrame(true);
	// animation->setLoops(1);
	_appear = CCAnimate::create(animation);
	_appear->retain();

	// Action Disappear
	_disappear = CCDelayTime::create(TIME_DISAPPEAR_MOUSE);
	_disappear->retain();

	// Action Dizzy
	animation = CCAnimation::create();
	for (int i = 1; i<=5; i++)
	{
		char szName[100] = { 0 };
		sprintf(szName, "obj_mouse_hit_0%i.png", i);
		frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(szName);
		animation->addSpriteFrame(frame);
	}
	animation->setDelayPerUnit(TIME_ANIMATION_MOUSEHIT / 5);
	animation->setRestoreOriginalFrame(true);
	_dizzy = CCAnimate::create(animation);
	_dizzy->retain();
}

void Mouse::appear(int atHole)
{
	this->setVisible(true);
	_canHit = true;
	_atHole = atHole;
	this->stopAllActions();
	CCFiniteTimeAction* sequence = CCSequence::create(		
		(CCAnimate*)Mouse::_appear->copy()->autorelease(),
		CCDelayTime::create(_timeAppear - TIME_ANIMATION_MOUSE),
		CCCallFuncN::create(this, callfuncN_selector(Mouse::animationAppearDone)),
		NULL);

	this->runAction(sequence);	
}

void Mouse::dizzy()
{
	_canHit = false;

	this->stopAllActions();
	CCFiniteTimeAction* sequence = CCSequence::create(  
		(CCAnimate*)Mouse::_dizzy->copy()->autorelease(),
		CCCallFuncN::create(this, callfuncN_selector(Mouse::mouseHideHit)),
		NULL);

	this->runAction(sequence);
}

void Mouse::animationAppearDone(CCNode* pSender)
{
	_canHit = false;

	this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("obj_mouse_06.png"));
	CCFiniteTimeAction* sequence = CCSequence::create(
		(CCDelayTime*)_disappear->copy()->autorelease(),
		CCCallFuncN::create(this, callfuncN_selector(Mouse::mouseHideMiss)),
		NULL);

	this->runAction(sequence);
}

void Mouse::mouseHideMiss(CCNode* pSender)
{
	pSender->setVisible(false);
	this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("obj_mouse_05.png"));

	_game->increaseMouseMiss();
	_game->setHoleBlank(_atHole);
	_atHole = -1;
}

void Mouse::mouseHideHit(CCNode* pSender)
{
	pSender->setVisible(false);
	this->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("obj_mouse_05.png"));
	_game->increaseMouseHit();
	_game->setHoleBlank(_atHole);
	_atHole = -1;
}

float Mouse::randomFloat(float a, float b)
{	
	float random = CCRANDOM_0_1();
	float diff = b - a;
	float r = random * diff;
	return a + r;
}

void Mouse::initTimeDelay(float timeDelay1, float timeDelay2)
{
	_timeDelay = randomFloat(timeDelay1, timeDelay2);
}

void Mouse::initTimeAppear(float timeAppear1, float timeAppear2)
{
	_timeAppear = randomFloat(timeAppear1, timeAppear2);
}
